/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oalam.bookstore.dao;

import com.oalam.bookstore.domain.books.Author;
import com.oalam.bookstore.domain.books.Book;
import com.oalam.bookstore.domain.BusinessObjectFactory;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import com.sun.org.apache.xpath.internal.XPathAPI;
import java.io.File;
import java.util.List;

/**
 *
 * @author Administrateur
 */
public class FileBookDao implements BookDao {

    private String dbPath;
    private Document doc;
    private final BusinessObjectFactory factory = BusinessObjectFactory.getInstance();

    public FileBookDao(String dbPath) {
        this.dbPath = dbPath;
        this.doc = parseXmlFile(dbPath, false);
    }

    private Document parseXmlFile(String filename, boolean validating) {
        try {
            // Create a builder factory
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(validating);

            // Create the builder and parse the file
            Document doc = factory.newDocumentBuilder().parse(new File(filename));
            return doc;
        } catch (SAXException e) {
            // A parsing error occurred; the xml input is not valid
        } catch (ParserConfigurationException e) {
        } catch (IOException e) {
        }
        return null;
    }

    private List<Book> findBookByXPathExpression(String xpath) {
        List<Book> result = new ArrayList<Book>();

        try {
            // Get the matching elements
            NodeList nodelist = XPathAPI.selectNodeList(doc, xpath);

            // Process the elements in the nodelist
            for (int i = 0; i < nodelist.getLength(); i++) {
                // Get element
                Element elem = (Element) nodelist.item(i);
                String bookIsbn = elem.getAttribute("ISBN");
                String bookTitle = "";
                String bookAuthor = "";
                NodeList child = elem.getChildNodes();

                for (int j = 0; j < child.getLength(); j++) {
                    try {
                        Element n = (Element) child.item(j);
                        if (n != null) {
                            if (n.getNodeName().equals("title")) {
                                bookTitle = n.getTextContent();
                            }
                            if (n.getNodeName().equals("author")) {
                                bookAuthor = n.getTextContent();
                            }
                        }
                    } catch (java.lang.ClassCastException e) {
                    }

                }

                // book instanciation stuff
                Book b = factory.createBook(bookIsbn);
                Author a = factory.createAuthor(bookAuthor);
                b.setTitle(bookTitle);
                b.setAuthor(a);
                result.add(b);
            }

        } catch (Exception e) {

            e.printStackTrace();

        }
        return result;
    }

    @Override
    public List<Book> findBookByTitle(String title) {
        String xpath = "//book[contains(title,'" + title + "')]";
        return findBookByXPathExpression(xpath);
    }

    @Override
    public List<Book> findBookByAuthor(Author a) {
        String xpath = "//book[contains(author,'" + a.getName() + "')]";
        return findBookByXPathExpression(xpath);
    }
}
